﻿package com.tudou.home.newskin.panel
{
    import com.tudou.events.*;
    import com.tudou.home.newskin.events.*;
    import com.tudou.js.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.ui.*;

    public class KeyboardExternalPanel extends Sprite implements IVolume, ITime, IPlayer
    {
        private var volume:Number;
        private var playTime:Number;
        private var isVolumeOff:Boolean;
        private var totalTime:Number;
        private var canDo:Boolean = false;

        public function KeyboardExternalPanel()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, addStageHlr);
            return;
        }// end function

        private function callTogglePause(event:JSEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2[0] ? (true) : (false);
            _loc_3 = false;
            togglePause(_loc_3);
            return;
        }// end function

        public function ready() : void
        {
            canDo = false;
            return;
        }// end function

        public function paused() : void
        {
            canDo = true;
            return;
        }// end function

        private function addJsCall() : void
        {
            var jsExchanger:* = JSExchanger.getInst();
            jsExchanger.addEventListener("togglePause", callTogglePause);
            jsExchanger.addEventListener("volumeUp", function (event:Event) : void
            {
                callVolume(0.15);
                return;
            }// end function
            );
            jsExchanger.addEventListener("volumeDown", function (event:Event) : void
            {
                callVolume(-0.15);
                return;
            }// end function
            );
            jsExchanger.addEventListener("seekPlaytime", callSeekPlayHlr);
            return;
        }// end function

        private function togglePause(param1:Boolean) : void
        {
            if (!canDo)
            {
                return;
            }
            dispatchEvent(new SkinEvent(SkinEvent.TOGGLE_PAUSE, param1));
            return;
        }// end function

        private function callSeekPlayHlr(event:JSEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2[0];
            var _loc_4:* = Number(_loc_2[1]);
            if (_loc_3 == "seek")
            {
                callSeekTime(_loc_4);
            }
            return;
        }// end function

        public function getMute() : Boolean
        {
            return isVolumeOff;
        }// end function

        public function getVolume() : Number
        {
            return volume;
        }// end function

        private function addStageHlr(event:Event) : void
        {
            this.stage.addEventListener(KeyboardEvent.KEY_UP, keyDownHandler);
            addJsCall();
            return;
        }// end function

        public function completed() : void
        {
            canDo = false;
            return;
        }// end function

        private function callVolume(param1:Number) : void
        {
            dispatchEvent(new SkinEvent(SkinEvent.END_SET_VOLUME, MathUtil.unit(volume + param1)));
            return;
        }// end function

        public function setMute(param1:Boolean) : void
        {
            isVolumeOff = param1;
            return;
        }// end function

        public function setVolume(param1:Number) : void
        {
            volume = param1;
            return;
        }// end function

        private function callSeekTime(param1:Number) : void
        {
            var _loc_2:Number = NaN;
            if (!canDo)
            {
                return;
            }
            if (param1 < 1)
            {
                _loc_2 = param1;
            }
            else
            {
                _loc_2 = param1 / totalTime;
            }
            if (_loc_2 < 0)
            {
                _loc_2 = 0;
            }
            if (_loc_2 > 1)
            {
                _loc_2 = 1;
            }
            dispatchEvent(new SkinEvent(SkinEvent.END_SEEK, MathUtil.unit(_loc_2)));
            return;
        }// end function

        private function keyDownHandler(event:KeyboardEvent) : void
        {
            switch(event.keyCode)
            {
                case Keyboard.SPACE:
                {
                    togglePause(false);
                    break;
                }
                case Keyboard.UP:
                {
                    callVolume(0.15);
                    break;
                }
                case Keyboard.DOWN:
                {
                    callVolume(-0.15);
                    break;
                }
                case Keyboard.LEFT:
                {
                    callSeek(-10);
                    break;
                }
                case Keyboard.RIGHT:
                {
                    callSeek(10);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function setPlayTime(param1:Number) : void
        {
            playTime = param1;
            return;
        }// end function

        public function playing() : void
        {
            canDo = true;
            return;
        }// end function

        private function callSeek(param1:Number) : void
        {
            if (!canDo)
            {
                return;
            }
            var _loc_2:* = (playTime + param1) / totalTime;
            dispatchEvent(new SkinEvent(SkinEvent.END_SEEK, MathUtil.unit(_loc_2)));
            return;
        }// end function

        public function buffering() : void
        {
            canDo = true;
            return;
        }// end function

        public function loading() : void
        {
            canDo = false;
            return;
        }// end function

        public function setTotalTime(param1:Number) : void
        {
            totalTime = param1;
            return;
        }// end function

    }
}
